<?php
declare (strict_types = 1);

namespace app\api\controller;

use app\common\controller\Api;
use think\Request;

class SpecificationValue extends Api
{
    //列表接口
    public function index()
    {
        if (input('post.limit')) {
            $this->limit = input('post.limit');
        }
        $post = array_filter(input('post.'));
        if(isset($post['where']) && is_array($post['where'])){
            foreach ($post['where'] as $k=>$v){
                if($k == 'name'){
                    $this->where[] = [$k,'like','%'.$v.'%'];
                }elseif($k == 'user_index_id'){
                    if(!isset($this->userinfo) || empty($this->userinfo)){
                        $this->checkLogin();
                    }
                    $this->where[] = [$k,'=',$this->userinfo['id']];
                }else{
                    $this->where[] = [$k,'=',$v];
                }
            }
        }
        $order = $post['order'] ?? [];
        if (!empty($order)) {
            if (is_string($order)) {
                if (strstr($order, ',')) {
                    $order = explode(',', $order);
                    $arr = [];
                    foreach ($order as $k => $v) {
                        $arr[$v] = 'desc';
                    }
                    $order = $arr;
                }
            }
        } elseif(!empty($this->order)){
            $order = $this->order;
        } else {
            $order = 'id desc';
        }
        $this->where[] = ['delete_time','=',null];
        if ($this->select) {
            $list = $this->model->with($this->with[request()->controller()]['function'] ?? [])->order($order)->where($this->where)->select();
        } else {
            if(input('find')){
                $list = $this->model->with($this->with[request()->controller()]['function'] ?? [])->order($order)->where($this->where)->find();
            }else{

                $list = $this->model->with($this->with[request()->controller()]['function'] ?? [])->order($order)->where($this->where)->paginate($this->limit);
            }
        }
        if (!empty($list)) {
            $list = $list->toArray();
            $list = $this->index_handel($list);
            if(isset($this->with[request()->controller()]['function'])){
                if(isset($list['total'])){
                    //多条数据三数组以上，有分页
                    foreach ($this->with[request()->controller()]['function'] as $v){
                        foreach ($list['data'] as $kk=>$vv){
                            urladd($list['data'][$kk][$v]);
                        }
                    }
                }elseif(isset($list[0])){
                    //多条数据二数组以上，无分页
                    foreach ($this->with[request()->controller()]['function'] as $v){
                        foreach ($list as $kk=>$vv){
                            urladd($list[$kk][$v]);
                        }
                    }
                }else{
                    //1条数据，一维数组
                    foreach ($this->with[request()->controller()]['function'] as $v){
                        urladd($list[$v]);
                    }
                }
            }
            urladd($list);
        }
        return datasuccess($list);
    }
}
